<!DOCTYPE html>
<html>
<title>JavaScript 函数重复使用示例 - 基础教程(cainiaojc.com)</title>

<body>
    <P>1</P>
    <p>我们可以根据需要多次重复使用greet()函数：</p>
    <p id="wm"></p>

    <script>
        var text;

        function great() {
            text = "Hello,world"
            document.write(text);
            document.getElementById("wm").innerHTML = text;
        }


        great();
        great();
        great();
        great();
        great();
    </script>


    <br>
    <br>
    <P>2</P>
    <p>"?!"的值通过name参数传递到函数中：</p>
    <script>
        function great1(name) {
            document.write("Pwq," + "不对" + name);
        }
        great1("?!");
    </script>


    <br>
    <br>
    <P>3</P>
    <p>定义多个参数:</p>
    <script>
        function add(num1, num2, num3) {
            var total = num1 + num2 + num3;
            document.writeln(total);
        }
        add(5, 6, 10);
        add(-5, -6, -10);
    </script>

    <br>
    <br>
    <p>4</p>
    <script>
        function showfullname(fname, lname) {
            document.write(fname + " " + lname + "<br>");
        }

        showfullname("kv", "366");
        showfullname("dev"); // 输出: John undefined
    </script>

    <br>
    <br>
    <p>5</p>
    <p>返回值</p>
    <script>
        function square(number) {
            return number * number;
        }
        var x = square(5);
        document.write(x);
    </script>

    <br>
    <br>
    <p>6</p>
    <p>返回值数组</p>
    <script>
        function makeArray() {
            var myArray = ["Apple", "Mango", "Banana", "Orange"];
            return myArray;
        }
        var arr = makeArray();
        for (let i = 0; i < arr.length; i++) {
            document.write(arr[i] + "<br>");
        }
    </script>

    <br>
    <br>
    <p>7</p>
    <p>函数表达式</p>
    <script>
        var square = function(number) {
            return number * number
        }
        var x = square(5);
        document.write(x);
    </script>

    <br>
    <br>
    <p>8</p>
    <p>函数作用范围,myFunc()外变量city 是未定义的.</p>
    <script>
        myFunc();

        function myFunc() {
            var city = "New Delhi";
            document.write(typeof city + " " + city + "<br>");
        }
        document.write(typeof city);
    </script>
</body>

</html>